package com.geezdata.cps.sdk3rd.miya.request;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.hibernate.validator.constraints.NotBlank;

import com.geezdata.cps.base.utils.Md5Util;
import com.geezdata.cps.sdk3rd.miya.constant.Constants;
import com.geezdata.cps.sdk3rd.miya.response.MiyaConfirmOrderResult;


public class MiyaConfirmOrderRequest extends MiyaRequest<MiyaConfirmOrderResult> {
	@NotBlank(message = "orderId 不能为空！")
    private String orderId;
    
	{
        this.methodName = Constants.METHOD_MIYA_ORDER_CONFIRM;
    }
    
    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    private void generateSign(){
        this.timestamp = Long.toString(System.currentTimeMillis()/1000);
        HashMap<String, String> hashMap = new HashMap<String, String>();     
        hashMap.put("timestamp", timestamp);
        hashMap.put("vendor_key", vendorKey);
        hashMap.put("method", methodName);
        hashMap.put("order_id", orderId);
        Object[] key_arr = hashMap.keySet().toArray();  
        StringBuffer forsignBuffer = new StringBuffer();
        Arrays.sort(key_arr);  
        for  (Object key : key_arr) {  
            Object value = hashMap.get(key); 
            forsignBuffer.append(key.toString()+value.toString());
        }          
        forsignBuffer.append(secretKey);
        String sign = Md5Util.md5(forsignBuffer.toString());
        this.setSign(sign);
    }

    @Override
    public Map<String, Object> getAppParams() {
        this.generateSign();
        Map<String, Object> httpParams = new HashMap<String, Object>();
        httpParams.put("secret_key", secretKey);
        httpParams.put("vendor_key", vendorKey);
        httpParams.put("method", methodName);
        httpParams.put("timestamp", timestamp);
        httpParams.put("sign", this.getSign());
        httpParams.put("order_id", orderId);
        return httpParams;
    }

    @Override
    public Class<MiyaConfirmOrderResult> getResponseClass() {   
        return MiyaConfirmOrderResult.class;
    }
}
